#!/usr/bin/env python3

import hashlib
import sys

def calculate_sha256(file_path):
    sha256_hash = hashlib.sha256()
    with open(file_path, 'rb') as file:
        # 逐块更新哈希对象
        for chunk in iter(lambda: file.read(4096), b""):
            sha256_hash.update(chunk)
    return sha256_hash.hexdigest()

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: calculate_sha256.py <file_path>")
        sys.exit(1)

    file_path = sys.argv[1]
    try:
        sha256_hash = calculate_sha256(file_path)
        print(f"{file_path}\n{sha256_hash}")
    except FileNotFoundError:
        print(f"{file_path} 不存在")
        sys.exit(1)
